package com.lei.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lei.servicebase.exception.GuliException;
import com.lei.ucenter.entity.Member;
import com.lei.ucenter.entity.vo.RegisterVo;
import com.lei.ucenter.mapper.MemberMapper;
import com.lei.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lei.ucenter.util.BCryptUtil;
import com.lei.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-13
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //登录方法
    @Override
    public String login(Member member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"账户名和密码不能为空");
        }
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member memberData = baseMapper.selectOne(wrapper);
        //判断该用户名是否存在
        if (memberData==null){
            throw new GuliException(20001,"用户名或密码错误");
        }
        //判断密码是否错误
        if (!BCryptUtil.match(password,memberData.getPassword())){
            throw new GuliException(20001,"用户名或密码错误");
        }
        //判断是否禁用
        if (memberData.getIsDisabled()){
            throw new GuliException(20001,"该账号已被禁用");
        }
        //返回token字符串
        return JwtUtils.getJwtToken(memberData.getId(),memberData.getNickname());

    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>=1){
            throw new GuliException(20001,"该账号已被注册");
        }

        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(password)||StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"有必填项为空");
        }

        String  redisCode= redisTemplate.opsForValue().get("yzm:" + mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误");
        }

        Member member = new Member();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(BCryptUtil.encodePassword(password));
        member.setIsDisabled(false);
        member.setAvatar("https://i02piccdn.sogoucdn.com/36f725ac1bca4d71");
        baseMapper.insert(member);
    }

    // 根据wx的openid 进行查询
    @Override
    public Member getOpenIdMember(String openid) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        Member member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
